from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from meiduo_admin.serializers.skus import SKUGoodsSerializer, SKUCategorieSerializer, SPUSimpleSerializer, SPUSpecSerializer
from meiduo_admin.utils import UserPageNum
from goods.models import SKU, GoodsCategory, SPU, SPUSpecification
from rest_framework.generics import ListAPIView


class SKUGoodsView(ModelViewSet):
    #  指定序列化器
    serializer_class = SKUGoodsSerializer
    #  指定分页器
    pagination_class = UserPageNum

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')

        if keyword == '' or keyword is None:
            return SKU.objects.all()
        else:
            return SKU.objects.filter(name__contains=keyword)

class SKUCategorieView(ListAPIView):
    serializer_class = SKUCategorieSerializer

    #  根据规律存储规律parent_id大于37为三级分类信息
    queryset = GoodsCategory.objects.filter(parent_id__gt=37)

class SPUSimpleView(ListAPIView):
    serializer_class = SPUSimpleSerializer
    queryset = SPU.objects.all()


class SPUSpecView(ListAPIView):
    serializer_class = SPUSpecSerializer

    def get_queryset(self):
        #  获取spu_id的值
        pk = self.kwargs['pk']
        #  根据spu的id值关联过滤查询出规格信息
        return SPUSpecification.objects.filter(spu_id=self.kwargs['pk'])